package com.example.ui.contact;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.NetContact;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.chat.SharePreferenceUtil;
import com.example.ui.leftmenu.Item08View;
import com.example.util.Util;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddTelActivity extends Activity{

	private Context context;
	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;
	private EditText editText;
	private String num ;
	private SharePreferenceUtil mSpUtil;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.chat_add_tel);
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		editText = (EditText) findViewById(R.id.add_editText);
		titleLeftBtn.setText("返回");
		titleText.setText("手机号码添加");		
		titleRightBtn.setText("确定");
		num = editText.getText().toString();
		mSpUtil = HSApplication.getInstance().getSpUtil();

		titleLeftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});


		titleRightBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.toast(context, "onClick 确定");
				if(CheckTel()){
					AddRequest();
				}else{
					L.showShort(context, "号码有误,非通信录中的号码");
				}
			}
		});		
	}
	
	public boolean CheckTel(){
		
		for(int i=0;i<Item08View.contacts.size();i++){
			String telNum = Item08View.contacts.get(i).getNum();
			if(num.equals(telNum)){
				return true;
			}
		}
		return false;
	}
    private static AsyncHttpClient client = new AsyncHttpClient();
	public void AddRequest(){
    	RequestParams params = new RequestParams();
        
		params.put("type", "1");//手机类型标识，1代表android手机，2代表ios
		params.put("fromMobile", mSpUtil.getMobile());
		params.put("toMobile", num);             //curNum
		client.post(NetContact.LOAD_FRIEND_REQUEST_URL,
				params, new JsonHttpResponseHandler() {
	
					
					@Override
					public void onSuccess(JSONObject response) {
						L.Debug("AddRequest response", response.toString());
						JSONArray jsonArray;
						try {
							jsonArray = response.getJSONArray("response");
							L.Debug("AddRequest jsonArray response", jsonArray.toString());

						} catch (JSONException e) {
							e.printStackTrace();
							L.Debug("AddRequest JSONException", e.getMessage());
						}
								
					}

				});		
	}
}
